/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.management.user.web;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.tompai.common.constants.ReturnStatusConstant;
import com.tompai.common.enums.OperateStatusEnums;
import com.tompai.common.model.SysPermission;
import com.tompai.common.model.SysRolePermissionRelation;
import com.tompai.management.user.service.SysPermissionService;
import com.tompai.management.user.service.SysRolePermissionRelationService;
import com.tompai.management.user.web.vo.SysPermissionInfoQueryVo;
import com.tompai.management.user.web.vo.SysPermissionInfoVo;
import com.tompai.management.user.web.vo.SysPermissionTreeVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 权限信息管理
 * 
 * @author liinux@qq.com
 * @date 2019/10/21
 */
@Slf4j
@Controller
@Api(tags = "权限信息管理{用户管理:/user/manager}")
@RequestMapping("/user/permission")
public class PermissionController {

	@Autowired
	private SysPermissionService sysPermissionService;

	@Autowired
	private SysRolePermissionRelationService sysRolePermissionRelationService;

	@ApiOperation(value = "获取权限列表")
	@GetMapping
	public String sysPermissionInfo() {
		return "user/sys-permission-info";
	}

	@ApiOperation(value = "查询指定权限信息")
	@GetMapping("/get-sys-permission-info/{id}")
	@ResponseBody
	public SysPermission getSysPermissionInfo(@PathVariable("id") @ApiParam(name = "id", value = "主键id") String id) {
		return sysPermissionService.getSysPermissionInfo(id);
	}

	@ApiOperation(value = "查询所有的权限信息")
	@GetMapping("/get-all-sys-permission-info")
	@ResponseBody
	public SysPermissionTreeVo getAllSysPermissionInfo(String roleId) {
		SysPermissionTreeVo sysPermissionTreeVo = new SysPermissionTreeVo();
		sysPermissionTreeVo.setList(sysPermissionService.selectSysPermissionInfo(new SysPermission()));
		List<SysRolePermissionRelation> sysRolePermissionRelations = sysRolePermissionRelationService
				.selectSysPermissionRelationRoleInfo(roleId);
		sysPermissionTreeVo.setCheckedId(sysRolePermissionRelations.stream()
				.map(SysRolePermissionRelation::getPermissionId).collect(Collectors.toList()));
		return sysPermissionTreeVo;
	}

	@ApiOperation(value = "查询所有权限角色信息")
	@GetMapping("/select-sys-permission-info")
	@ResponseBody
	public SysPermissionInfoVo selectSysPermissionInfo(SysPermission sysPermission, int page, int limit) {
		PageInfo<SysPermission> pageInfo = sysPermissionService.selectSysPermissions(sysPermission, page, limit);
		SysPermissionInfoVo sysPermissionInfoVo = new SysPermissionInfoVo();
		sysPermissionInfoVo.setCode(OperateStatusEnums.SUCCESS.getStringValue());
		sysPermissionInfoVo.setCount(Long.valueOf(pageInfo.getTotal()).intValue());
		sysPermissionInfoVo.setData(pageInfo.getList());
		return sysPermissionInfoVo;
	}

	@ApiOperation(value = "添加/编辑权限权限信息")
	@PostMapping("/add-or-update-sys-permission-info")
	@ResponseBody
	public ReturnStatusConstant addOrUpdateSysPermissionInfo(
			@RequestBody SysPermissionInfoQueryVo sysPermissionInfoQueryVo) {
		ReturnStatusConstant returnCode = new ReturnStatusConstant();
		try {

			if (StringUtils.hasText(sysPermissionInfoQueryVo.getId())) {
				sysPermissionInfoQueryVo.beforeUpdate();
				sysPermissionService.update(sysPermissionInfoQueryVo);
			} else {
				sysPermissionInfoQueryVo.beforeInsert();
				sysPermissionService.insert(sysPermissionInfoQueryVo);
			}

			returnCode.setCode(ReturnStatusConstant.SUCCESS);//
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_MESSAGE);
		} catch (Exception e) {
			log.error("添加/编辑权限权限信息：", e);
			returnCode.setCode(ReturnStatusConstant.FAIL);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_FAIL);
		}
		return returnCode;
	}

	@ApiOperation(value = "删除权限权限信息")
	@PostMapping("/delete-sys-permission-info")
	@ResponseBody
	public ReturnStatusConstant deleteSysPermissionInfo(
			@RequestParam("id") @ApiParam(name = "id", value = "主键id") String id) {
		ReturnStatusConstant returnCode = new ReturnStatusConstant();
		try {
			sysPermissionService.deleteByPrimaryKey(id);
			returnCode.setCode(ReturnStatusConstant.SUCCESS);//
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_MESSAGE);
		} catch (Exception e) {
			log.error("删除信息出错：", e);
			returnCode.setCode(ReturnStatusConstant.FAIL);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_FAIL);
		}
		return returnCode;
	}

	@ApiOperation(value = "根据swagger扫描更新权限列表")
	@GetMapping("/update-permission-by-swagger")
	@ResponseBody
	@ApiIgnore
	public Boolean updatePermissionBySwagger() {
		return sysPermissionService.updatePermissionBySwagger();
	}
}
